<script>
	import utils from '@/static/js/utils.js'
	import store from '@/static/js/store.js'

	export default {
		onLaunch: function() {
			console.log('App Launch')
			// 获取 token
			const token = utils.http.getToken()
			const expireTime = utils.http.getExpireTime()
			if (token && expireTime > 0) {

				console.log("登录状态保持中")

				// token时限判断
				if (expireTime > new Date().getTime()) {
					utils.http.get("/user/profile").then(res => {
						if (res.status == 200) {
							// utils.globalData.setUserInfo(res.data)
							store.commit('setUserInfo', res.data)
						}
					})

					// 1个小时执行一次判断
					setInterval(() => {
						let time = utils.http.getExpireTime()
						if (time - new Date().getTime() < 60 * 1000 * 60 * 24) {
							utils.http.get("/login/changeToken").then(res => {
								if (res.status == 200) {
									utils.setToken(res.data.token)
									utils.setExpireTime(res.data.expireTime)
								}
							})
						}
					}, 60 * 1000 * 60 * 1)

				} else {
					// token过期
					utils.http.removeTokenAndExpireTime()
					console.log("登录过期")
				}

			} else {
				console.log("未登录")
			}
		},
		onShow: function() {
			console.log('App Show')
		},
		onHide: function() {
			console.log('App Hide')
		}
	}
</script>

<style lang="scss">
	/* 注意要写在第一行，同时给style标签加入lang="scss"属性 */
	@import "uview-ui/index.scss";
	@import "uview-ui/theme.scss";
	@import "@/static/iconfont.css";
	@import "@/static/css/app.scss";

	/*每个页面公共css */
	page {
		height: 100%;
	}
</style>
